<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'活码列表'),
			array('op'=>'edit','title'=>'添加活码'),
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_code',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
			$item['url']=custom_url('jy_scrm/code//id='.$item['id'],true);
			$item['code']=pdo_getcolumn('jy_scrm_contact_way',array('merchid'=>$_SESSION['onemerchid'],'config_id'=>$item['config_id'],'uniacid'=>$_SESSION['uniacid']),'qr_code');
			if($item['usertype']==1){
				$item['worker']=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$item['worker_userid'],'uniacid'=>$_SESSION['uniacid']),'name');
			}else{
				$item['workers']=$item['workers']?json_decode($item['workers'],true):array();
				$item['worker']=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$item['workers'][0],'uniacid'=>$_SESSION['uniacid']),'name');
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>
		1、可以生成带参数的二维码名片，支持活码功能，即随机选取设置的活码成员推给用户。加企业微信为好友后，可以给微信联系人自动回复相应欢迎消息和打标签。</br>
		2、每创建一个渠道活码，该码则自动进入【内容引擎】--【图片类型】--分组【渠道码-企业微信】，以素材的方式通过聊天侧边栏快速发送给客户。</br>
		3、受限于官方，单人类型的渠道码创建后尽量不要再修改成员，否则会造成列表中，该二维码中间的头像与配置的成员头像不一致，但是并不影响功能使用。</br>
		4、如果企业在企业微信后台为相关成员配置了可用的欢迎语，使用第三方系统配置欢迎语，则均不起效，推送的还是企业微信官方的。
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'活码名称','type'=>'string'),
			array('name'=>'url','title'=>'活码二维码','type'=>'ewm'),
			array('name'=>'code','title'=>'企业二维码','type'=>'image'),
			array('name'=>'groupid','title'=>'分组','type'=>'select','options'=>'groups'),
			array('name'=>'worker','title'=>'活码成员','type'=>'string'),
			array('name'=>'need_auth','title'=>'自动添加好友','type'=>'radio','options'=>'need_auths'),
			array('name'=>'tagid','title'=>'标签','type'=>'select','options'=>'tags'),
			array('name'=>'buystatus','title'=>'客户数','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'活码名称','type'=>'string','width'=>'15'),
			array('name'=>'usertype','title'=>'跟进状态','type'=>'radio','options'=>'usertype','width'=>'15'),
		),	
	);
	$return['usertype']=array(1=>'单人',2=>'多人');
	$return['need_auths']=array('直接通过','需要验证');
	$return['tags']=$this->getall('member_group_tag',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['groups']=$this->getall('code_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
$steps=array('基础设置','引流成员设置','欢迎语设置');
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
			'title'=>$postdata['title'],
			'need_auth'=>intval($postdata['need_auth']),
			'groupid'=>intval($postdata['groupid']),
			'tagid'=>intval($postdata['tagid']),
			'opentype'=>intval($postdata['opentype']),
			'opentimes'=>ijson_encode($postdata['opentimes']),
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_scrm_code',$data,array('merchid'=>$_SESSION['onemerchid'],'id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_scrm_code',$data);
			$id=pdo_insertid();
		}
		exi('','','/code.list/content/id='.$id);
	}	
	if($id){
		$item=pdo_get('jy_scrm_code',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();	
	}
	$item['opentimes']=$item['opentimes']?json_decode($item['opentimes'],true):array('hour'=>array());
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'基础设置',
		'submit_title'=>'保存并下一步',
		'edit'=>array(
			array('type'=>'string','title'=>'活码名称','name'=>'title','help'=>''),
			array('type'=>'select','title'=>'选择分组','name'=>'groupid','options'=>'groups','help'=>''),	
			array('type'=>'switch','title'=>'需要验证添加好友?','name'=>'need_auth'),
			array('type'=>'switch','title'=>'选择时间段？','name'=>'opentype'),
			array(
							'type'=>'array',
							'name'=>'opentimes',
							'add_tip'=>'添加一个时间段',
								'list'=>array(
									array('type'=>'string','name'=>'hour.start','title'=>'开始时间'),
									array('type'=>'string','name'=>'hour.end','title'=>'结束时间'),
									
								),
								'details'=>array(
									array('type'=>'time_hour','name'=>'hour','title'=>'时间段'),								
								)
			),
			array('type'=>'choose','title'=>'客户标签','name'=>'tagid','action'=>'jy_scrm/member.get_tag/','help'=>''),
		),
	);
	$return['usertype']=array(1=>'单人',2=>'多人');
	$return['groups']=pdo_getall('jy_scrm_code_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['htmls'][]=array('type'=>'steps','steps'=>$steps,'step'=>'1');
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='content'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if($postdata['usertype']==1){
			if(!$postdata['worker_userid']){
				exi('请选择成员！','error');
			}
			$workers=array(
				0=>$postdata['worker_userid']
			);
		}else{
			$workers=array();
			foreach($postdata['workers'] as $wv){
				if($wv['userid']){
					$workers[]=$wv['userid'];
				}
			}
			if(!$workers[0]){
				exi('至少选择一个成员！','error');
			}
			if(!$postdata['party']){
				exi('选择一个部门！','error');
			}
		}
		$data=array(
			'usertype'=>intval($postdata['usertype']),
			'teshu_timeopen'=>intval($postdata['teshu_timeopen']),
			'user_maxopen'=>intval($postdata['user_maxopen']),
			'user_max'=>intval($postdata['user_max']),
			'teshu_times'=>ijson_encode($postdata['teshu_times']),
			'workers'=>ijson_encode($postdata['workers']),
			'party'=>intval($postdata['party']),
			'worker_userid'=>$postdata['worker_userid']
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_scrm_code',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_scrm_code',$data);
			$id=pdo_insertid();
		}
		exi('','','/code.list/set/id='.$id);
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_code',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();	
	}
	$item['usertype']=$item['usertype']?:1;
	$item['teshu_times']=$item['teshu_times']?json_decode($item['teshu_times'],true):array();
	$item['workers']=$item['workers']?json_decode($item['workers'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'引流成员设置',
		'submit_title'=>'保存并下一步',
		'edit'=>array(
			array('type'=>'radio','title'=>'类型','name'=>'usertype','options'=>'usertype'),
			array('type'=>'select','name'=>'party','options'=>'bumens','option_value'=>'bumenid','title'=>'选择部门','show'=>array('usertype'=>'2')),
			array('type'=>'select','name'=>'worker_userid','options'=>'workers','option_title'=>'name','option_value'=>'userid','show'=>array('usertype'=>'1'),'title'=>'企业成员(单人生效)'),
			array(
							'type'=>'array',
							'name'=>'workers',
							'show'=>array('usertype'=>'2'),
							'title'=>'企业成员(多人生效)',
							'add_tip'=>'添加一个企业成员',
								'list'=>array(
									array('type'=>'string','name'=>'start','title'=>'开始时间'),
									array('type'=>'string','name'=>'end','title'=>'结束时间'),
									array('type'=>'select','name'=>'userid','options'=>'workers','option_title'=>'name','option_value'=>'userid','title'=>'企业成员'),
									array('type'=>'checkbox','name'=>'xingqi','options'=>'weeks','title'=>'适用周期'),
								),
								'details'=>array(
									array('type'=>'time_onehour','name'=>'start','title'=>'开始时间'),
									array('type'=>'time_onehour','name'=>'end','title'=>'结束时间'),
									array('type'=>'select','name'=>'userid','options'=>'workers','option_title'=>'name','option_value'=>'userid','title'=>'企业成员'),
									array('type'=>'checkbox','name'=>'xingqi','options'=>'weeks','title'=>'适用周期'),
								)
			),
			array('type'=>'switch','title'=>'特殊时期','name'=>'teshu_timeopen'),
			array(
							'type'=>'array',
							'name'=>'teshu_times',
							'title'=>'企业成员',
							'add_tip'=>'添加一个企业成员',
								'list'=>array(
									array('type'=>'string','name'=>'start','title'=>'开始时间'),
									array('type'=>'string','name'=>'end','title'=>'结束时间'),
									array('type'=>'select','name'=>'workerid','options'=>'workers2','option_title'=>'name','title'=>'企业成员'),
								),
								'details'=>array(
									array('type'=>'time_range','name'=>'days','title'=>'开始时间'),
									array('type'=>'time_onehour','name'=>'start','title'=>'开始时间'),
									array('type'=>'time_onehour','name'=>'end','title'=>'结束时间'),
									array('type'=>'select','name'=>'workerid','options'=>'workers2','option_title'=>'name','title'=>'企业成员'),
								)
			),
			array('type'=>'switch','title'=>'员工添加上限','name'=>'user_maxopen'),
			array('type'=>'string','title'=>'员工上限数','name'=>'user_max'),
		),
	);
	$return['usertype']=array(1=>'单人',2=>'多人');
	$return['weeks']=$this->weeks;
	$return['bumens']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	$return['workers']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name','userid'),'userid');
	$return['workers2']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name','userid'),'id');
	$return['htmls'][]=array('type'=>'steps','steps'=>$steps,'step'=>'2');
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
			'welcome'=>$postdata['welcome'],
			'welcomeid'=>$postdata['welcomeid'],
			'endid'=>$postdata['endid'],
		);
		$id=intval($postdata['id']);
		if($id){
			$item=pdo_get('jy_scrm_code',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			$item['workers']=$item['workers']?json_decode($item['workers'],true):array();
			if($item['usertype']==1){
				$workers=$item['worker_userid'];
			}else{
				$workers=array();
				foreach($item['workers'] as $wv){
					$workers[]=$wv['userid'];
				}
			}
			$state='jy_scrm|mid|0|code|'.$id;
			$params=array(
				'type'=>$item['usertype'],
				'scene'=>2,
				'style'=>$item['style']?:0,
				'remark'=>$item['title'],
				'skip_verify'=>$item['need_auth']?false:true,
				'state'=>$state,
				'user'=>$workers,
				'party'=>$item['party'],
				'is_temp'=>$item['is_temp']?true:false,
				'expires_in'=>0,
				'chat_expires_in'=>0,
				'unionid'=>'',
			);
			if($item['conclusions']){
				$params['conclusions']=ijson_encode($item['conclusions']);
			}
			//$params['config_id']=$item['config_id'];
			$res=model('core/wxwork_userapi')->add_contact_way($params);
			$data['config_id']=$res['config_id'];
			pdo_update('jy_scrm_code',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$params['config_id']=$res['config_id'];
			$params['qr_code']=$res['qr_code'];
			$params['status']=$res['status'];
			$params['user']=ijson_encode($params['user']);
			$this->model('worker')->add_contact_way($params);
		}
		exi('设置成功','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_code',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();	
	}
	$item['workers']=$item['workers']?json_decode($item['workers'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'欢迎语设置',
		'submit_title'=>'保存',
		'edit'=>array(
			array('type'=>'content','name'=>'welcomeid','title'=>'选择欢迎语'),
			array('type'=>'content','name'=>'endid','title'=>'选择结束语'),
		),
	);
	$return['htmls'][]=array('type'=>'steps','steps'=>$steps,'step'=>'3');
	$return['htmls'][]=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$config_id=pdo_getcolumn('jy_scrm_code',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'config_id');
			if($config_id){
				pdo_delete('jy_scrm_contact_way',array('config_id'=>$config_id,'uniacid'=>$_SESSION['uniacid']));
				$this->model('worker')->del_contact_way($config_id);
			}
			pdo_delete('jy_scrm_code',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_code',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！','','list');
}